package cn.dakaqi.services.impl;

import cn.dakaqi.dao.PassportApplyOrderPatchDao;
import cn.dakaqi.entities.PassportApplyOrderPatch;
import cn.dakaqi.services.PassportApplyOrderPatchService;
import cn.dakaqi.utils.exception.ServiceRuntimeException;
import com.google.common.collect.Maps;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.persistence.DynamicSpecifications;
import org.springside.modules.persistence.SearchFilter;

import java.util.List;
import java.util.Map;

/**
 * Copyright: Copyright (c)2016
 * Company: 志愿者打卡器(www.dakaqi.cn)
 * 类名称: PassportApplyOrderPatchServiceImpl <br>
 * 类描述: <br>
 *
 * @author: ChunYang.Zhang
 * @since: 2016/10/25 16:12
 * @version: 1.0.0
 */
@Component
@Transactional
public class PassportApplyOrderPatchServiceImpl implements PassportApplyOrderPatchService
{
    @Autowired
    PassportApplyOrderPatchDao passportApplyOrderPatchDao;
    @Override
    public void save(PassportApplyOrderPatch passportApplyOrderPatch) throws ServiceRuntimeException
    {
        this.passportApplyOrderPatchDao.save(passportApplyOrderPatch);
    }

    @Override
    public PassportApplyOrderPatch findByOrderId(Long orderId) throws ServiceRuntimeException
    {
        Map<String, SearchFilter> filters = Maps.newHashMap();
        filters.put("passportApplyOrderId", new SearchFilter("passportApplyOrderId", SearchFilter.Operator.EQ, orderId));
        Specification<PassportApplyOrderPatch> spec = DynamicSpecifications.bySearchFilter(filters.values(), PassportApplyOrderPatch.class);
        List<PassportApplyOrderPatch> list = this.passportApplyOrderPatchDao.findAll(spec);
        if(null != list && list.size()>0)
            return list.get(list.size()-1);
        return null;
    }
}
